package xyz.xtt.exchange.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.exchange.entity.Test;
import xyz.xtt.exchange.mapper.TestMapper;
import xyz.xtt.exchange.service.ITestService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {
	@Override
	public Test selectForUpdate(Long id) {
		LambdaQueryChainWrapper<Test> query = new LambdaQueryChainWrapper<>(baseMapper);
		query.eq(Test::getId, id).last("for update");
		return query.oneOpt().orElseThrow(() -> new IllegalStateException("arget"));
	}

	@Override
	public boolean updateTarget(Long id, String name) {
		LambdaUpdateChainWrapper<Test> update = new LambdaUpdateChainWrapper<>(baseMapper);
		return update.eq(Test::getId, id).set(Test::getName, name).update();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean lockUpdate(Long id, String name) {
		Test selectForUpdate = selectForUpdate(id);
		return updateTarget(id, name);
	}
}
